/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.six.gestion.usuarios;

import com.sig.utilerias.FuncionesValidacion;
import com.sig.utilerias.entity.EntityManagerFactory;
import com.six.dto.GcliInfPersona;
import com.six.dto.GcliUsuario;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.NotifyDescriptor.Confirmation;

final class UsuariosPanel extends javax.swing.JPanel {

    private final UsuariosOptionsPanelController controller;
    private long idUsuario = -1;
    private final MyListener listener = new MyListener();

    UsuariosPanel(UsuariosOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        // TODO listen to changes in form fields and call controller.changed()
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtUsuarios = new javax.swing.JTable();
        jbAgregar = new javax.swing.JButton();
        jbAceptar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfNombre = new javax.swing.JTextField();
        jtfApPaterno = new javax.swing.JTextField();
        jtfApMaterno = new javax.swing.JTextField();
        jtfCorreo = new javax.swing.JFormattedTextField();
        jtfUsuario = new javax.swing.JTextField();
        jtfPassword = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        jchkAdministrador = new javax.swing.JCheckBox();

        jPanel1.setPreferredSize(new java.awt.Dimension(600, 300));

        jtUsuarios.setModel(new UsuariosPanel.MyModel());
        jtUsuarios.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jtUsuarios);

        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/six/gestion/usuarios/resources/agregar.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jbAgregar, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jbAgregar.text")); // NOI18N
        jbAgregar.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jbAgregar.setPreferredSize(new java.awt.Dimension(40, 40));
        jbAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAgregarActionPerformed(evt);
            }
        });

        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/six/gestion/usuarios/resources/aceptar.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jbAceptar, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jbAceptar.text")); // NOI18N
        jbAceptar.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jbAceptar.setPreferredSize(new java.awt.Dimension(40, 40));
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel4.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel5.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel6.text")); // NOI18N

        jtfNombre.setDocument(new com.sig.utilerias.text.JTextFieldLimit(100));
        jtfNombre.setText(org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtfNombre.text")); // NOI18N

        jtfApPaterno.setDocument(new com.sig.utilerias.text.JTextFieldLimit(100));
        jtfApPaterno.setText(org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtfApPaterno.text")); // NOI18N

        jtfApMaterno.setDocument(new com.sig.utilerias.text.JTextFieldLimit(100));
        jtfApMaterno.setText(org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtfApMaterno.text")); // NOI18N

        jtfCorreo.setDocument(new com.sig.utilerias.text.JTextFieldLimit(100));
        jtfCorreo.setText(org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtfCorreo.text")); // NOI18N

        jtfUsuario.setDocument(new com.sig.utilerias.text.JTextFieldLimit(8));
        jtfUsuario.setText(org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtfUsuario.text")); // NOI18N

        jtfPassword.setDocument(new com.sig.utilerias.text.JTextFieldLimit(8));
        jtfPassword.setText(org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtfPassword.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jLabel7.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jchkAdministrador, org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jchkAdministrador.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(73, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(186, 186, 186)
                        .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfNombre)
                            .addComponent(jtfApPaterno)
                            .addComponent(jtfApMaterno)
                            .addComponent(jtfCorreo)
                            .addComponent(jtfUsuario)
                            .addComponent(jtfPassword)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jchkAdministrador)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfApPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfApMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jchkAdministrador))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtfUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jtfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(31, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAgregarActionPerformed
        clearFields();
        this.jtfNombre.requestFocus();
        this.idUsuario = -1;
    }//GEN-LAST:event_jbAgregarActionPerformed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        Logger.getLogger(UsuariosPanel.class.getName()).warning("Guardar usuario");

        if (validaCamposObligatorios()) {
            EntityManager em = EntityManagerFactory.getEntityManager();
            if (em == null) {
                Confirmation message = new NotifyDescriptor.Confirmation("Error en la base de datos!!",
                        NotifyDescriptor.ERROR_MESSAGE);
                DialogDisplayer.getDefault().notify(message);
                return;
            }

            EntityTransaction transaction = em.getTransaction();
            try {
                transaction.begin();
                TypedQuery<GcliUsuario> query = em.createNamedQuery("GcliUsuario.findByIdUsuario", GcliUsuario.class);
                query.setParameter("idUsuario", this.idUsuario);
                List<GcliUsuario> list = query.getResultList();
                GcliUsuario usuario;

                System.out.println("Size::" + list.size());
                if (list.size() > 0) {
                    usuario = list.get(0);
                } else {
                    usuario = new GcliUsuario();
                }
                usuario.setUsuario(jtfUsuario.getText());
                usuario.setPassword(String.valueOf(jtfPassword.getPassword()));
                usuario.setBAdministrador(jchkAdministrador.isSelected());
                if (usuario.getIdPersona() == null) {
                    usuario.setIdPersona(new GcliInfPersona());
                }
                usuario.getIdPersona().setNombre(jtfNombre.getText());
                usuario.getIdPersona().setApellidoPaterno(jtfApPaterno.getText());
                usuario.getIdPersona().setApellidoMaterno(jtfApMaterno.getText());
                usuario.getIdPersona().setCorreoElectronico(jtfCorreo.getText());
                GcliUsuario merge = em.merge(usuario);
                transaction.commit();
                recargarTabla(em);
                idUsuario = merge.getIdUsuario();
            } catch (Exception e) {
                Confirmation message = new NotifyDescriptor.Confirmation("Error al guardar usuario. ERROR:: " + e.getMessage(),
                        NotifyDescriptor.ERROR_MESSAGE);
                DialogDisplayer.getDefault().notify(message);
                Logger.getLogger(UsuariosPanel.class.getName()).log(Level.SEVERE, "Error al guardar usuario. ERROR:: {0}", e.getMessage());
            }
        } else {
            Confirmation message = new NotifyDescriptor.Confirmation("Problema Médico no puede estar vacio, favor de verificar!!",
                    NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(message);
        }
    }//GEN-LAST:event_jbAceptarActionPerformed

    void load() {
        Logger.getLogger(UsuariosPanel.class.getName()).warning("Se cargan los usuarios");
        EntityManager em = EntityManagerFactory.getEntityManager();
        this.idUsuario = -1;

        if (em == null) {
            Confirmation message = new NotifyDescriptor.Confirmation("Error en la base de datos!!",
                    NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(message);
            return;
        }
        recargarTabla(em);
    }

    private void recargarTabla(EntityManager em) {
        final TypedQuery<GcliUsuario> query = em.createNamedQuery("GcliUsuario.findAll", GcliUsuario.class);

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                System.out.println("Inicia:RUN:" + new Date());
                jtUsuarios.getSelectionModel().removeListSelectionListener(listener);
                @SuppressWarnings("unchecked")
                List<GcliUsuario> usuarios = query.getResultList();
                if (usuarios != null && !usuarios.isEmpty()) {
                    MyModel modelo = (MyModel) jtUsuarios.getModel();
                    modelo.addRows(usuarios);
                }
                jtUsuarios.getSelectionModel().addListSelectionListener(listener);
                System.out.println("Fin:RUN:" + new Date());
            }
        });
    }

    void store() {
        // TODO store modified settings
        // Example:
        // Preferences.userNodeForPackage(UsuariosPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
        // or for org.openide.util with API spec. version >= 7.4:
        // NbPreferences.forModule(UsuariosPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
        // or:
        // SomeSystemOption.getDefault().setSomeStringProperty(someTextField.getText());
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JCheckBox jchkAdministrador;
    private javax.swing.JTable jtUsuarios;
    private javax.swing.JTextField jtfApMaterno;
    private javax.swing.JTextField jtfApPaterno;
    private javax.swing.JFormattedTextField jtfCorreo;
    private javax.swing.JTextField jtfNombre;
    private javax.swing.JPasswordField jtfPassword;
    private javax.swing.JTextField jtfUsuario;
    // End of variables declaration//GEN-END:variables

    private void clearFields() {
        jtfNombre.setText("");
        jtfApPaterno.setText("");
        jtfApMaterno.setText("");
        jtfCorreo.setText("");
        jtfUsuario.setText("");
        jtfPassword.setText("");
        jchkAdministrador.setSelected(false);
    }

    private void setFields(int index) {
        GcliUsuario usuario = ((MyModel) jtUsuarios.getModel()).getValueAt(index);
        this.idUsuario = usuario.getIdUsuario();
        this.jtfNombre.setText(usuario.getIdPersona().getNombre());
        jtfApPaterno.setText(usuario.getIdPersona().getApellidoPaterno());
        jtfApMaterno.setText(usuario.getIdPersona().getApellidoMaterno());
        jtfCorreo.setText(usuario.getIdPersona().getCorreoElectronico());
        jtfUsuario.setText(usuario.getUsuario());
        jtfPassword.setText("88888888");
        jchkAdministrador.setSelected(usuario.getBAdministrador());
    }

    private boolean validaCamposObligatorios() {
        return FuncionesValidacion.esValido(jtfNombre.getText()) && FuncionesValidacion.esValido(jtfApPaterno.getText())
                && FuncionesValidacion.esValido(jtfCorreo.getText()) && FuncionesValidacion.esValido(jtfUsuario.getText())
                && FuncionesValidacion.esValido(String.valueOf(jtfPassword.getPassword()));
    }

    private class MyListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            System.err.println("ListSelectionEvent" + e);
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();

            if (lsm.isSelectionEmpty()) {
                Logger.getLogger(UsuariosOptionsPanelController.class.getName()).log(Level.SEVERE, "Sin datos");
            } else {
                int seleccionIndex = lsm.getMinSelectionIndex();
                System.err.println("SeleccionIndex::" + seleccionIndex);
                setFields(seleccionIndex);
            }
        }
    }

    private class MyModel extends AbstractTableModel implements Serializable {

        private final List<GcliUsuario> usuarios = new ArrayList<GcliUsuario>();

        public void addRows(List<GcliUsuario> usuarios) {
            this.usuarios.clear();
            this.usuarios.addAll(usuarios);
            fireTableRowsInserted(0, this.usuarios.size());
        }

        @Override
        public String getColumnName(int column) {
            return org.openide.util.NbBundle.getMessage(UsuariosPanel.class, "UsuariosPanel.jtUsuarios.columnModel.title0");
        }

        @Override
        public int getRowCount() {
            return usuarios.size();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            GcliUsuario usuario = usuarios.get(rowIndex);
            GcliInfPersona persona = usuario.getIdPersona();

            return persona.getNombre() + " " + persona.getApellidoPaterno() + " " + persona.getApellidoMaterno();
        }

        private GcliUsuario getValueAt(int index) {
            if (index < this.usuarios.size()) {
                return this.usuarios.get(index);
            }
            return null;
        }

    }
}
